function [datasets buckets] = partitionData(data, subsets)
    datasets=[];    
    classes = unique(data(:,1));
    classNo = size(classes,1);

    #rows: classes, cols: occurencies in given part
    buckets = zeros(classNo,subsets);
    order   = randperm(size(data,1));


    for(i=order)
        cl=data(i,1);
        #find the bucket with minimal occurencies of given class
        [no bucket] = min( buckets(cl+1,:)' );
        divRow = sum(sum(buckets(:,1:bucket-1)),2);        
        datasets=[datasets(1:divRow,:);data(i,:);datasets(divRow+1:end,:)];
        buckets(cl+1,bucket)+=1;       
    endfor;
    
#    first=order(1);
#    order(1)=[];

#    datasets(:,:,subsets)=data(first,:);
#    size(datasets);
    

#    for(i = order)
#        cl = data(i,1);
#        #no. of occurencies of current class examples in all subsets        
#        subsetSizes=[];
#        for(subset=1:subsets)
#            cl;
#            subsetSizes(subset,1)=sum(datasets(:,:,subset)(:,1)==cl);
#        endfor;
#        [occr sset] = min(subsetSizes)
#        size(datasets(:,:,sset))
#        size([datasets(:,:,sset);data(i,:)])        
#        datasets(size(datasets(:,:,sset),1)+1,:,sset)=data(i,:);
#    endfor;
end;
